<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-if="userType !== 'qudao'"
        v-model="listQuery.query_type"
        style="width: 150px"
        class="filter-item"
        @change="onChangeType"
      >
        <el-option
          v-for="item in typeOptions"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-if="listQuery.query_type !== 1 && userType !== 'qudao'"
        v-model="listQuery.username"
        clearable
        filterable
        style="width: 150px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option
          v-for="(item, index) in dailiArr"
          :key="index"
          :label="item"
          :value="item"
        />
      </el-select>
      
      <el-date-picker
        v-model="value1"
        type="daterange"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item pickertime"
        format="yyyy-MM-dd"
        value-format="timestamp"
        :picker-options="pickerOptions2"
        @change="handleFilter"
      />
      <a-button type="primary" size="large" @click="handleFilter">
        搜索
      </a-button>
      <a-button type="default" size="large" @click="handleCancel">
        重置
      </a-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
    >
      <el-table-column v-if="userType == 'all'" key="1235" min-width="20" type="expand" @click="c">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="oppo_uv:">
              <span>{{ props.row.oppo_uv }}</span>
            </el-form-item>
            <el-form-item label="vivo_uv:">
              <span>{{ props.row.vivo_uv }}</span>
            </el-form-item>
            <el-form-item label="xiaomi_uv:">
              <span>{{ props.row.xiaomi_uv }}</span>
            </el-form-item>
            <el-form-item label="huawei_uv:">
              <span>{{ props.row.huawei_uv }}</span>
            </el-form-item>

            <el-form-item label="oppo_pv:">
              <span>{{ props.row.oppo_pv }}</span>
            </el-form-item>
            <el-form-item label="vivo_pv:">
              <span>{{ props.row.vivo_pv }}</span>
            </el-form-item>
            <el-form-item label="xiaomi_pv:">
              <span>{{ props.row.xiaomi_pv }}</span>
            </el-form-item>
            <el-form-item label="huawei_pv:">
              <span>{{ props.row.huawei_pv }}</span>
            </el-form-item>

            <el-form-item label="oppo_ex:">
              <span>{{ props.row.oppo_ex }}</span>
            </el-form-item>
            <el-form-item label="vivo_ex:">
              <span>{{ props.row.vivo_ex }}</span>
            </el-form-item>
            <el-form-item label="xiaomi_ex:">
              <span>{{ props.row.xiaomi_ex }}</span>
            </el-form-item>
            <el-form-item label="huawei_ex:">
              <span>{{ props.row.huawei_ex }}</span>
            </el-form-item>

            <el-form-item label="oppo uv点击率:">
              <span>{{ (props.row.oppo_ex / props.row.oppo_uv * 100).toFixed(2)}} %</span>
            </el-form-item>
            <el-form-item label="vivo uv点击率:">
              <span>{{ (props.row.vivo_ex / props.row.vivo_uv * 100).toFixed(2)}} %</span>
            </el-form-item>
            <el-form-item label="xiaomi uv点击率:">
              <span>{{ (props.row.xiaomi_ex / props.row.xiaomi_uv * 100).toFixed(2)}} %</span>
            </el-form-item>
            <el-form-item label="huawei uv点击率:">
              <span>{{ (props.row.huawei_ex / props.row.huawei_uv * 100).toFixed(2)}} %</span>
            </el-form-item>

          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        key="1"
        label="日期"
        align="center"
        prop="time_"
        sortable="custom"
        min-width="120"
      >
      </el-table-column>
      <el-table-column
        v-if="listQuery.query_type !== 1"
        key="2"
        prop="username"
        :label="listQuery.query_type === 2 ? '代理账号' : '渠道账号'"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        v-if="listQuery.query_type === 3"
        key="3"
        label="归属代理"
        min-width="120"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>{{ row.user_agency }}</span>
        </template>
      </el-table-column>
      <el-table-column
        key="4"
        prop="true_uv"
        sortable
        label="启动UV"
        min-width="120"
        align="center"
      ></el-table-column>

      <!-- <el-table-column
        v-if="listQuery.query_type !== 2 && userType === 'all'"
        key="6"
        prop="supplement_uv"
        label="补量UV"
        min-width="120"
        align="center"
      ></el-table-column> -->
      <!-- <el-table-column
        v-if="userType === 'all'"
        key="7"
        label="补量后uv"
        prop="sum_uv"
        sortable="custom"
        min-width="130"
        align="center"
      ></el-table-column> -->
      <!-- <el-table-column
        v-if="listQuery.query_type === 1 && userType === 'all'"
        key="8"
        label="百度UV"
        prop="baidu_uv"
        min-width="120"
        align="center"
      ></el-table-column> -->
      <el-table-column
        key="9"
        label="启动PV"
        prop="pv"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        key="10"
        label="万次UV单价"
        prop="price"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        key="11"
        label="支出"
        min-width="120"
        prop="expend"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        v-if="userType == 'all' || qd_List.indexOf(username) > -1"
        key="39"
        prop="ex"
        label="d"
        min-width="120"
        align="center"
      ></el-table-column>

       <el-table-column
        v-if="userType == 'all' || qd_List.indexOf(username) > -1"
        key="34"
        prop="ex"
        label="uv点击率"
        min-width="120"
        align="center"
      >
      <template slot-scope="item">
        {{ (item.row.ex / item.row.true_uv * 100).toFixed(2)}} %
      </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        min-width="80"
        class-name="small-padding fixed-width"
        v-if="userType != 'qudao' && listQuery.query_type == 1"
      >
        <template slot-scope="item">
          <a-button type="link" size="small" @click="showSub(item.row)">
            查看下级
          </a-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      class="pageShow"
      :page.sync="listQuery.page"
      :page_size.sync="listQuery.page_size"
      @pagination="getList"
    />
    <pagination
      v-show="total > 0"
      layout="prev, pager, next"
      :small="true"
      class="pageHidden"
      :total="total"
      :page.sync="listQuery.page"
      :page_size.sync="listQuery.page_size"
      @pagination="getList"
    />
    <!-- 子账户数据 -->
    <el-dialog title="下级每日数据" :visible.sync="dialogVisible" width="80%">
      <div>
        <div style="padding-bottom: 10px">
          <el-input
            v-model.trim="sublistQuery.qudao"
            placeholder="请输入下级账号进行搜索"
            style="width: 220px"
            class="filter-item"
            @keyup.enter.native="showSub"
          />
          <el-button
            plain
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="showSub"
            >搜索</el-button
          >
        </div>
        <el-table
          :key="tableKey"
          v-loading="subListLoading"
          :data="subList"
          fit
          highlight-current-row
          style="width: 100%"
          @sort-change="sortChange"
        >
          <el-table-column
            key="1"
            label="日期"
            align="center"
            prop="time_"
            sortable="custom"
            min-width="120"
          ></el-table-column>
          <el-table-column
            key="2"
            prop="username"
            :label="listQuery.query_type === 2 ? '代理账号' : '渠道账号'"
            min-width="120"
            align="center"
          ></el-table-column>
          <el-table-column
            v-if="listQuery.query_type !== 2"
            key="4"
            prop="true_uv"
            sortable
            label="启动UV"
            min-width="120"
            align="center"
          ></el-table-column>

          <el-table-column
            key="9"
            label="启动PV"
            prop="pv"
            min-width="120"
            align="center"
          ></el-table-column>
          <!-- <el-table-column key="10" label="万次UV单价" prop="price" min-width="120" align="center"></el-table-column> -->
          <el-table-column
            key="111"
            label="单价"
            min-width="120"
            prop="price"
            align="center"
          >
          <template slot-scope="item">
            {{item.row.price_ip ? item.row.price_ip: item.row.price }}
          </template>
          </el-table-column>
          <el-table-column
            key="11"
            label="花费"
            min-width="120"
            prop="expend"
            align="center"
          ></el-table-column>
          <el-table-column
            v-if="userType == 'all' || qd_List.indexOf(username) > -1"
            key="40"
            prop="ex"
            label="d"
            min-width="120"
            align="center"
          ></el-table-column>
          <el-table-column
            v-if="userType == 'all' || qd_List.indexOf(username) > -1"
            key="30"
            prop="ex"
            label="uv点击率"
            min-width="120"
            align="center"
          ><template slot-scope="item">
        {{ (item.row.ex / item.row.true_uv * 100).toFixed(2)}} %
      </template>
      </el-table-column>
      <!-- <el-table-column
            v-if="userType == 'daili'"
            key="139"
            prop="ex"
            label="操作"
            min-width="120"
            align="center"
          ><template slot-scope="item">
        <el-button type="pirmay" size="mini" @click="priceCompute(item.row)">计算单价</el-button>
      </template>
      </el-table-column> -->
        </el-table>
        <pagination
          :total="subTotal"
          class="pageShow"
          :page.sync="sublistQuery.page"
          :page_size.sync="sublistQuery.page_size"
          @pagination="showSub"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getProxyNameList,
  getChannelNameList,
  getDataReport,
  subordinate,
} from "@/api/admin/admin";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import {qd_List} from '@/utils/index'
export default {
  name: "Report",
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      dialogVisible: false,
      userType: this.$store.state.user.type,
      username: this.$store.state.user.name.username,
      tableKey: 0,
      list: null,
      subList: [],
      total: 0,
      listLoading: true,
      subListLoading: true,
      value1: "",
      dailiArr: [],
      countPv: "",
      countUv: "",
      countProfit: "",
      feedback_num: "",
      listQuery: {
        page: 1,
        page_size: 20,
        query_type: 1,
        username: "",
        order_field: "",
        sort: "",
        time_start: "",
        time_end: "",
      },
      sublistQuery: {
        page: 1,
        page_size: 20,
        date: "",
        qudao: "",
      },
      subTotal: 0,
      pickerOptions2: {
        shortcuts: [
          {
            text: "昨天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      typeOptions: [],
      qd_List: qd_List
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    if (this.userType == "daili") {
      this.listQuery.query_type = 1;
      this.typeOptions = [
        { label: "按总和查看", key: 1 },
        { label: "按渠道查看", key: 3 },
      ];
    } else if (this.userType == "qudao") {
      this.listQuery.query_type = 3;
    } else {
      this.typeOptions = [
        { label: "按总和查看", key: 1 },
        { label: "按代理查看", key: 2 },
        { label: "按渠道查看", key: 3 },
      ];
    }
  },
  methods: {
    showSub(item) {
      // 获取下级实时数据
      this.dialogVisible = true;
      this.subListLoading = true;
      if (item.time_) {
        this.sublistQuery.date = item.time_;
      }
      if (item.page) {
        this.sublistQuery.page = item.page;
        this.sublistQuery.page_size = item.limit;
      }
      subordinate(this.sublistQuery).then((res) => {
        this.subList = res.DATA.list;

        this.subListLoading = false;
        this.subTotal = res.DATA.total;
      });
    },
    getList(item) {
      this.listLoading = true;
      if (this.value1) {
        this.listQuery.time_start = this.value1[0] / 1000;
        this.listQuery.time_end = this.value1[1] / 1000;
      } else {
        this.listQuery.time_start = "";
        this.listQuery.time_end = "";
      }
      if (item) {
        this.listQuery.page = item.page;
        this.listQuery.page_size = item.limit;
      }

      if (this.listQuery.query_type === 1) {
        this.listQuery.username = "";
      }
      getDataReport(this.listQuery).then((response) => {
        // debugger;
        this.list = response.DATA.list;
        this.total = response.DATA.total;
        if (this.listQuery.query_type == 1) {
          let today = response.DATA.today;
          this.list.unshift({
            baidu_uv: 0,
            expend: today.day_expend,
            feedback: 0,
            id: 0,
            price: "--",
            pv: today.pv,
            ex: today.ex,
            sum_uv: 51860,
            supplement_uv: 0,
            time_: response.DATA.datetime,
            true_uv: today.uv,
            user_agency: "",
            username: "",
          });
        }

        // this.dailiArr = response.channel
        // this.countPv = response.count_pv
        // this.countUv = response.count_uv
        // this.countProfit = response.count_profit
        // this.feedback_num = response.feedback_num
        this.listLoading = false;
      });
    },
    //切换查看类型
    onChangeType() {
      // console.log('11111111111', this.listQuery.query_type)
      this.listQuery.username = "";
      if (this.listQuery.query_type == 2) {
        this.getProxyNameList();
      } else if (this.listQuery.query_type == 3) {
        this.getChannelNameList();
      }
      this.handleFilter();
    },
    //获取代理列表
    getProxyNameList() {
      getProxyNameList().then((r) => {
        this.dailiArr = r.DATA;
      });
    },
    //获取渠道列表
    getChannelNameList() {
      getChannelNameList().then((r) => {
        this.dailiArr = r.DATA;
      });
    },
    getSummaries() {
      if (this.listQuery.query_type === 1) {
        const sums = [
          "总计",
          "-",
          "-",
          "-",
          this.countUv,
          "-",
          this.countPv,
          "-",
          this.countProfit,
          "-",
          this.feedback_num,
          "-",
        ];
        return sums;
      } else if (this.listQuery.query_type === 2) {
        const sums = [
          "总计",
          "-",
          this.countUv,
          this.countPv,
          "-",
          this.countProfit,
        ];
        return sums;
      } else {
        const sums = [
          "总计",
          "-",
          "-",
          "-",
          "-",
          "-",
          this.countUv,
          this.countPv,
          "-",
          this.countProfit,
          this.feedback_num,
          "-",
        ];
        return sums;
      }
    },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.listQuery.order_field = "day";
        if (order === "ascending") {
          this.listQuery.sort = "asc";
        } else if (order === "descending") {
          this.listQuery.sort = "desc";
        } else {
          this.listQuery.order_field = "day";
          this.listQuery.sort = "desc";
        }
        this.handleFilter();
      } else if (prop === "uv") {
        this.listQuery.order_field = "uv";
        if (order === "ascending") {
          this.listQuery.sort = "asc";
        } else if (order === "descending") {
          this.listQuery.sort = "desc";
        } else {
          this.listQuery.order_field = "day";
          this.listQuery.sort = "desc";
        }
        this.handleFilter();
      } else if (prop === null) {
        this.listQuery.order_field = "day";
        this.listQuery.sort = "desc";
        this.handleFilter();
      }
    },
    sortByID(order) {
      if (order === "ascending") {
        this.listQuery.sort = "asc";
      } else {
        this.listQuery.sort = "desc";
      }
      this.handleFilter();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleCancel() {
      this.value1 = "";
      this.listQuery.sort = "";
      this.listQuery.page = 1;
      this.listQuery.query_type = 1;
      this.getList();
    },
    priceCompute(item){
      let click_number = item.ex
      let url = 'http://houtai.ydwhgs.com/#/danjia/index?click='+click_number
       window.open(url, "_blank");
    }
  }
};
</script>

<style scoped>
.demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 24%;
  }
.pageHidden {
  display: none;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .pickertime {
    width: 100%;
  }
}
.filter-item {
  margin-bottom: 3px;
}
</style>
